package tp4;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class Abin<T> {
private noeudAbin<T> n ;
static public Writer w;


public Abin(noeudAbin<T> n) {

	this.n = n;
}
public Abin() {


}

@Override
public String toString() {
	return "Abin [n=" + n + "]";
}
public Abin(T[] tas) {

	this.n = tas(tas, 0);
}

private noeudAbin<T> tas(T[] tas , int pos) {

	if (pos  < tas.length){
	noeudAbin<T> N =  new noeudAbin<T>( tas[pos],new Abin<T>( tas(tas,pos*2 +1)) ,new Abin<T>(tas(tas,pos*2 +2)) );


	return N;
	}else{
		
	return null;
	}
}

public int hauteur(){
	if( n == null ){
		return 0;
	}else {
		return 1 +  Math.max(n.getDroite().hauteur(),n.getDroite().hauteur());
	}
}
public int largeur(){
	if( n == null ){
		return 0;
	}else {
		return 1 + n.getDroite().hauteur() + n.getDroite().hauteur();
	}
}
public void toGraph(String filename){
	try {
		this.w = new FileWriter(filename);
		GraphViz.declareGraph(w);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	
	this.parcours(1);
	
	GraphViz.endGraph(w);
	GraphViz.toImage(filename);
}
public void parcours(int  n ){
	if(this.n != null){
	System.out.println(n);

	GraphViz.declareNode(this.w,n , (String)this.n.getValue().toString());

	if(this.n.getGauche() != null ){
		if ( n != 0){
			
		GraphViz.declareEdge(this.w, n, 2*n+1);
		}
		 this.n.getGauche() .parcours(2*n+1);
	}
	if(this.n.getDroite() != null ){
		if ( n != 0){
		
		GraphViz.declareEdge(this.w, n, 2*n+2);
		}
		this.n.getDroite().parcours(2*n+2);
	}
	}else{
		GraphViz.declareNullNode(w, n);
	}
}
}
